<?php
namespace app\admin\logic;

use app\admin\model\Goods;
use think\facade\Request;
use think\facade\View;
//商品页
class GoodsLogic{
    //goods分类
    public static function goodCate(){
        $cate=Goods::gcate();
        $fenlei=[];
        foreach ($cate as $v){
            if ($v['parent_id']==0){
                $fenlei[]=$v;
            }
        }
        foreach ($fenlei as &$v){
            foreach ($cate as $s){
                if ($s['parent_id']==$v['cate_id']){
                    $v['er'][]=$s;
                }
            }
            if (!isset($v['er'])){
                $v['er']=1;
            }
        }
        foreach ($fenlei as &$v){
            if (is_array($v['er'])){
                foreach ($v['er'] as &$vv){
                    foreach ($cate as $s){
                        if ($s['parent_id']==$vv['cate_id']){
                            $vv['san'][]=$s;
                        }
                    }
                    if (!isset($vv['san'])){
                        $vv['san']=1;
                    }
                }
            }
        }

        return $fenlei;
    }
    //goods查询关键字变色
    public static function changeSe($list){
        $param=Request::param();
        if (isset($param['keywords']) &&!empty($param['keywords'])){
            foreach ($list as &$v){
                $word='<font color="pink" size="5">'.$param['keywords'].'</font>';
                $v['goods_name']=str_replace($param['keywords'],$word,$v['goods_name']);
            }
        }
        return $list;
    }
    //goods时间上下架
    public static function  goodsOrder($orderid){
        $order=['g.goods_id' => 'asc'];
        View::assign(['msg'=>'','order'=>1]);
        if(isset($orderid) && $orderid=='1'){
            $order=['g.add_time'=>'desc'];
            View::assign(['msg'=>'↓','order'=>2]);
//         View::assign('msg','↓');
//         View::assign('order','2');
        }
        if(isset($orderid) && $orderid=='2'){
            $order=['g.add_time'=>'asc'];
            View::assign(['msg'=>'↑','order'=>0]);
//          View::assign('msg','↑');
//          View::assign('order','0');
        }
        return $order;
    }
    //goods留值设置
    public static function setData(){
        $param=Request::param();
        $data=[
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
            'pinpai'=>isset($param['pinpai'])?$param['pinpai']:'',
            'mintime'=>isset($param['mintime'])?$param['mintime']:'',
            'maxtime'=>isset($param['maxtime'])?$param['maxtime']:'',
            'minmoney'=>isset($param['minmoney'])?$param['minmoney']:'',
            'maxmoney'=>isset($param['maxmoney'])?$param['maxmoney']:'',
            'cid'=>isset($param['cid'])?$param['cid']:'',
        ];
        return $data;
    }
    //goodswhere条件封装
      public static function  setWhere(){
        $param=Request::param();
        $where=[];
        if (isset($param['keywords']) &&!empty($param['keywords'])){
            $where[]=['g.goods_name','like','%'.$param['keywords'].'%'];
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            $where[]=['g.if_show','=',$param['if_show'][0]];
        }
        if (isset($param['recommended']) && $param['recommended']<>''){
            $where[]=['g.recommended','=',$param['recommended']];
        }
        if (isset($param['pinpai']) && !empty($param['pinpai'])){
            $where[]=['g.brand','=',$param['pinpai']];
        }

        if (isset($param['mintime']) && !empty($param['mintime'])){
            $mintime=strtotime($param['mintime']);
            $where[]=['g.add_time','>', $mintime];
        }
        if (isset($param['maxtime']) && !empty($param['maxtime'])){
            $maxtime=strtotime($param['maxtime']);
            $where[]=['g.add_time','<',$maxtime];
        }
        if (isset($param['minmoney']) && !empty($param['minmoney'])){
            $where[]=['g.price','>', $param['minmoney']];
        }
        if (isset($param['maxmoney']) && !empty($param['maxmoney'])){
            $where[]=['g.price','<',$param['maxmoney']];
        }
        if (isset($param['cid']) && !empty($param['cid'])){
            $where[]=['g.cate_id','=',$param['cid']];
        }

        return $where;

    }
        //商品添加
     public static function   GoodsAdd($post){
         $if_show=isset($post['if_show'])?$post['if_show']:'';
         $recom=isset($post['recom'])?$post['recom']:'';
         $recommended=isset($post['recommended'])?$post['recommended']:'';
         $data=[
             'goods_name'=>$post['name'],
             'price'=>$post['price'],
             'cate_id'=>$post['cid'],
             'add_time'=>time(),
             'brand'=>$post['pinpai'],
             'vip'=>$post['vip_price'],
             'spec_qty'=>$post['num'],
             'recom'=>$recom,
             'recommended'=>$recommended,
             'if_show'=>$if_show,
             'description'=>$post['content'],
             'tags'=>$post['tags']
         ];
         return $data;

         }


         //商品修改
     public static function   goodsEditLogic($post){
         $if_show=isset($post['if_show'])?$post['if_show']:'';
         $recom=isset($post['recom'])?$post['recom']:'';
         $recommended=isset($post['recommended'])?$post['recommended']:'';
         $data=[
             'goods_id'=>$post['gid'],
             'goods_name'=>$post['name'],
             'price'=>$post['price'],
             'cate_id'=>$post['cid'],
             'brand'=>$post['pinpai'],
             'vip'=>$post['vip_price'],
             'spec_qty'=>$post['num'],
             'recom'=>$recom,
             'recommended'=>$recommended,
             'if_show'=>$if_show,
             'description'=>$post['content'],
             'tags'=>$post['tags']
         ];
         return $data;


     }

}